
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  utility;

import java.util.Hashtable;
import java.util.Vector;

import logger.PoolLogger;

import pool.bean.Master_table_Bean;
import pool.bean.Property_details_Bean;
import control.RequestParameter;
import dbmanager.ChekInMaster;
import dbmanager.DBManager;

public class MasterPLOperations 
{
	RequestParameter req;
	String username,password;
	Property_details_Bean prop = new Property_details_Bean();
	Vector<Object> propVector = new Vector<Object>();
	Vector<Object> propUpdateVector = new Vector<Object>();
	Vector<Object> oldUpdateVector = new Vector<Object>();
	Vector<Object> deleteVector = new Vector<Object>();
	String master;
	
	public Hashtable<Object, Object> datahash = new Hashtable<Object, Object>();
	PoolLogger pl;
	
	public MasterPLOperations(String username, String password)
	{
		this.username = username;
		this.password = password;
		prop.initPool();
		req = new RequestParameter();
		req.setUsername(username);
		req.setPassword(password);
		req.setMessage("System Message");
		req.setVersion("11");
		
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Master Insertion
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
     * This method registers any master with the given type
     * @param master New master name
     * @param type type of master
     * @return true if master inserted else false
     */
    public boolean insertMasterType(String master, String type)
    {
    	boolean isEntered = false;    	
    	ChekInMaster check = new ChekInMaster();
    	
		try 
		{
			if(!check.isInMaster(master))
			{
				if(this.insertMaster(master))
				{
					this.setNewProperty(master, "type", type, "0");
					insertProperty();
					propVector.clear();
					isEntered = true;
				}
				else
				{
					isEntered = false;
				}
			}
			else
			{
				//already present
				isEntered = true;
			}
			
		} 
		catch (Exception e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
		return isEntered;
    }
    
    /**
     *  This method checks and inserts a master without type
     * @param master New master name
     * @return true if master inserted else false
     */
    public boolean insertMaster(String master)
    {
    	boolean isInMaster = false;
    	
    	ChekInMaster check = new ChekInMaster();
    	
    	if(!check.isInMaster(master))
    	{
    		Master_table_Bean master_bean = new Master_table_Bean();
    		master_bean.initPool();
    		master_bean.setNewMasterObject(master);
    		
			req.setRequest("master_table");
			req.setProcess("add");
			
			master_bean.setReqParam(req);
			//master_bean.insertMaster();
			master_bean.insert();
			
	    	if(check.isInMaster(master))
	    		isInMaster = true;
	    	else
	    		isInMaster = false;
	    	
		}
    	else
    	{
    		isInMaster = true;
    	}
    	
    	return isInMaster;
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Property Insertion
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * This method sets property, values, ref for inserting in a master 
     * @param master Master name for which new property to be inserted
     * @param property new property
     * @param value new property value
     * @param ref new property ref
     */
    public void setNewProperty(String master, String property, String value, String ref)
    {
    	Hashtable<Object, Object> new_hash = new Hashtable<Object, Object>();
    	new_hash.put("mid", master);
    	new_hash.put("pid", property);
    	new_hash.put("pv", value);
    	new_hash.put("ref", ref);
    	propVector.add(new_hash);
    }
       
    
    /**
     * This method inserts properties set by setNewProperty() method
     */
    public void insertProperty()
    {
    	req.setProcess("add");
        req.setRequest("property_details");
        prop.setId(master);
        prop.clearPropertyVector();
        prop.initPool();
        prop.setReqParam(req);
        prop.setPropertyVector(propVector);
        //prop.insertProperty();
        
        prop.insert();
        propVector.clear();
    }
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Property Updation
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * This method sets old property for update. It takes all params i.e master,property,value and ref.
     * @param master Master name for which property to be updated 
     * @param property old property
     * @param value old property value
     * @param ref old property ref
     */
    public void setOldUpdateProperty(String master, String property, String value, String ref)
    {
    	Hashtable<Object, Object> new_hash = new Hashtable<Object, Object>();
    	new_hash.put("mid", master);
    	new_hash.put("pid", property);
    	new_hash.put("pv", value);
    	new_hash.put("ref", ref);
    	
    	oldUpdateVector.add(new_hash);
    }
    
    /**
     * This method sets new property for update. It takes all params i.e master,property,value and ref. 
     * @param master Master name for which property to be updated 
     * @param property old property
     * @param value new property value
     * @param ref new property ref
     */
    public void setUpdateProperty(String master, String property, String value, String ref)
    {
    	Hashtable<Object, Object> new_hash = new Hashtable<Object, Object>();
    	new_hash.put("mid", master);
    	new_hash.put("pid", property);
    	new_hash.put("pv", value);
    	new_hash.put("ref", ref);
    	propUpdateVector.add(new_hash);
    }
    
    /**
     * This method sets old property for update. It takes all params i.e master,property and value.
     * @param master Master name for which property to be updated 
     * @param property old property
     * @param value old property value
     */
    public void setOldUpdateProperty(String master, String property, String value)
    {
    	Hashtable<Object, Object> new_hash = new Hashtable<Object, Object>();
    	new_hash.put("mid", master);
    	new_hash.put("pid", property);
    	new_hash.put("pv", value);
    	oldUpdateVector.add(new_hash);
     }
    
    /**
     * This method sets new property for update. It takes all params i.e master,property and value. 
     * @param master Master name for which property to be updated 
     * @param property old property
     * @param value new property value
     */
    public void setUpdateProperty(String master, String property, String value)
    {
    	Hashtable<Object, Object> new_hash = new Hashtable<Object, Object>();
    	new_hash.put("mid", master);
    	new_hash.put("pid", property);
    	new_hash.put("pv", value);
    	propUpdateVector.add(new_hash);
    }
    
    /**
     * This method updates property of master set in update vector
     */
    public void updateProperty()
    {
    	req.setProcess("mod");
        req.setRequest("property_details");
        prop.setId(master);
        prop.clearPropertyVector();
        prop.initPool();
        prop.setReqParam(req);
        System.out.println("Old vector is "+oldUpdateVector);
        System.out.println("New vector is "+propUpdateVector);
        prop.setNewPropertyVector(propUpdateVector);
        prop.setOldPropertyVector(oldUpdateVector);
        prop.update();
        propUpdateVector.clear();
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Property Deletion
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    /**
     * This method sets remove property of a master matching all params i.e. master,property,value and ref. 
     * @param master master name
     * @param property property
     * @param value value
     * @param ref ref
     */
    public void setremoveProperty(String master, String property, String value, String ref)
    {
    	Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
    	hash.put("mid", master);
    	hash.put("pid", property);
    	hash.put("pv", value);
    	hash.put("ref", ref);
    	
    	deleteVector.add(hash);
	}

    /**
     * This method sets remove property of a master matching params i.e. master,property and value
     * @param master master name
     * @param property property
     * @param value value
     */
    public void setremoveProperty(String master, String property, String value)
    {
    	Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
    	hash.put("mid", master);
    	hash.put("pid", property);
    	hash.put("pv", value);
    	deleteVector.add(hash);
	}
    
    /**
     * This method sets remove  property of a master matching params i.e. masterand property.
     * @param master master name
     * @param property property
     */
    public void setremoveProperty(String master, String property)
    {
    	Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
    	hash.put("mid", master);
    	hash.put("pid", property);
    	deleteVector.add(hash);
	}
    
    /**
     * This method removes properties of a master set in delete vector.
     */
    public void removeProperty()
    {
    	req.setProcess("del");
        req.setRequest("property_details");
        prop.setId(master);
        prop.clearPropertyVector();
        prop.initPool();
        prop.setReqParam(req);
        prop.setPropertyVector(deleteVector);
        prop.delete();
        deleteVector.clear();
    }
    
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Master Deletion
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    /**
     * This method deletes all properties of a master
     * @param master master name
     */
    public void deleteMaster(String master)
    {
    	Master_table_Bean master_bean = new Master_table_Bean();
		master_bean.initPool();
		master_bean.setNewMasterObject(master);
		
		RequestParameter req = new RequestParameter();
		req.setUsername(username);
		req.setMessage("System Message");
		req.setPassword(password);
		req.setRequest("master_table");
		req.setProcess("del");
		
		master_bean.setReqParam(req);
		master_bean.delete();
    }
    
    /**
     * This method sets master name for MasterPL operations
     * @param master
     */
	public void setMaster(String master) 
	{
	  this.master =master;
	}
    
	public static void main(String[] args) 
	{
		MasterPLOperations mpl = new MasterPLOperations("systemuser","admin");
		
		//mpl.insertMaster("master_new_field_td_a_att");
		//mpl.deleteMaster("nainesh_653");
		
		//mpl.setNewProperty("master_new_field_td_a", "type", "a", "1");
		//mpl.setNewProperty("master_new_field_td_a", "attribute", "master_new_field_td_a_att", "2");
		//mpl.setNewProperty("master_new_field_td_a_att", "?", "type", "3");
		//mpl.setNewProperty("master_new_field_td_a_att", "text", "type", "4");
		//mpl.insertProperty();		
		//mpl.removeProperty("type", "type","property","1");
		mpl.setMaster("mastername_input_att");
		mpl.setUpdateProperty("mastername_input_att", "value", "999");
		mpl.setOldUpdateProperty("mastername_input_att", "value", "422");
		//mpl.setOldUpdateProperty("NIRMA DET SOAP", "Category", "HOME CARE", "1");
		//mpl.setOldUpdateProperty("NIRMA DET SOAP", "Grade", "B", "1");
		mpl.updateProperty();		
	}
}
